package com.zklh.leetcode.top100;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 岛屿数量算法实现
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/10/13
 */
public class Solution200 {
    public int numIslands(char[][] grid) {
        int landNum = 0;
        Queue<int[]> queue = new LinkedList<>();

        int[][] directs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '1') {
                    landNum += 1;
                    queue.add(new int[]{i,j});
                    grid[i][j] = '0';
                    while (!queue.isEmpty()) {
                        int[] current = queue.poll();
                        for (int[] direct : directs) {
                            if (current[0]+direct[0] < grid.length && current[0]+direct[0] >= 0
                                    && current[1]+direct[1] < grid[i].length && current[1]+direct[1] >=0
                                    && grid[current[0]+direct[0]][current[1]+direct[1]] == '1') {
                                queue.add(new int[]{current[0]+direct[0], current[1]+direct[1]});
                                grid[current[0]+direct[0]][current[1]+direct[1]] = '0';
                            }
                        }
                    }
                }
            }
        }
        return landNum;
    }

    public static void main(String[] args) {
        Solution200 solution200 = new Solution200();
        /*char[][] grid = {
                {'1','1','1','1','0'},
                {'1','1','0','1','0'},
                {'1','1','0','0','0'},
                {'0','0','0','0','0'}
        };*/
/*        char[][] grid = {
                {'1','1','0','0','0'},
                {'1','1','0','0','0'},
                {'0','0','1','0','0'},
                {'0','0','0','1','1'}
        };*/
        char[][] grid = {{'1','1','1','1','1','0','1','1','1','1','1','1','1','1','1','0','1','0','1','1'},{'0','1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','0'},{'1','0','1','1','1','0','0','1','1','0','1','1','1','1','1','1','1','1','1','1'},{'1','1','1','1','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},{'1','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},{'1','0','1','1','1','1','1','1','0','1','1','1','0','1','1','1','0','1','1','1'},{'0','1','1','1','1','1','1','1','1','1','1','1','0','1','1','0','1','1','1','1'},{'1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','0','1','1'},{'1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','1','1','1','1'},{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},{'0','1','1','1','1','1','1','1','0','1','1','1','1','1','1','1','1','1','1','1'},{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},{'1','1','1','1','1','0','1','1','1','1','1','1','1','0','1','1','1','1','1','1'},{'1','0','1','1','1','1','1','0','1','1','1','0','1','1','1','1','0','1','1','1'},{'1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','1','0'},{'1','1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','0','0'},{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'}};
        System.out.println(solution200.numIslands(grid));
    }
}